/*
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.spice.servlet.dispatcher;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public 
class SpiceEntityResolver implements EntityResolver{

	
	private static 
	final Logger ENTITY_REGISTRY = LoggerFactory.getLogger(SpiceEntityResolver.class);
	
	private static 
	final String SCHEMA_CONFIGURATION_LOCATION = "schema.properties";
	
	
	private 
	String absolutePath;
	
	public 
	String getAbsolutePath() {
		return absolutePath;
	}
	
	public 
	SpiceEntityResolver(String absolutePath) {
		this.absolutePath = absolutePath;
	}
	
	public 
	Properties getSchemaProperties() throws IOException {
	
		Properties properties = new Properties();
		final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(SCHEMA_CONFIGURATION_LOCATION);
		properties.load(inputStream);
		
		return properties;
	}

	@Override
	public 
	InputSource resolveEntity(final String publicId, final String systemId) throws SAXException, IOException {
		ENTITY_REGISTRY.info("The system Id is " + systemId + " and the public id is " + publicId);
	
		final Properties properties = getSchemaProperties();
		if(systemId.contains("http://www.spicesframework.com/context/spice-context.xsd")){
			final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(properties.getProperty("http://www.spicesframework.com/context/spice-context.xsd"));
			final InputSource inputSource = new InputSource(inputStream);
			inputSource.setSystemId(systemId);
			inputSource.setPublicId(publicId);
			ENTITY_REGISTRY.info("The system Id of Input source is  " + inputSource.getSystemId() + " and the public id is " + inputSource.getPublicId());
			
			return inputSource;
		}else if(systemId.contains("http://www.spicesframework.com/jdbc/spice-jdbc.xsd")){
			final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(properties.getProperty("http://www.spicesframework.com/context/spice-jdbc.xsd"));
			final InputSource inputSource = new InputSource(inputStream);
			inputSource.setSystemId(systemId);
			inputSource.setPublicId(publicId);
			ENTITY_REGISTRY.info("The system Id of Input source is  " + inputSource.getSystemId() + " and the public id is " + inputSource.getPublicId());
			
			return inputSource;
		}else if(systemId.contains("http://www.spicesframework.com/view/spice-view.xsd")){
			final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(properties.getProperty("http://www.spicesframework.com/context/spice-view.xsd"));
			final InputSource inputSource = new InputSource(inputStream);
			inputSource.setSystemId(systemId);
			inputSource.setPublicId(publicId);
			ENTITY_REGISTRY.info("The system Id of Input source is  " + inputSource.getSystemId() + " and the public id is " + inputSource.getPublicId());
			
			return inputSource;
		}
		
		return null;
	}

}
